﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WG计划管理工具
{
    public partial class frmWGKCSearch : Form
    {
        string strSelectWGJY = "select 数量 from wgjy";
        SqlTools.SqlTools sqt;

        public frmWGKCSearch()
        {
            InitializeComponent();
        }

        private void btnAddKeywords_Click(object sender, EventArgs e)
        {
            if (cmbSearchFields.Text.Length > 0 && txtSearchKeywords.Text.Trim().Length > 0)
            {
                if (lstKeywords.Items.Count > 0)
                {
                    for (int i = 0; i < lstKeywords.Items.Count; i++)
                    {
                        if (lstKeywords.Items[i].ToString().StartsWith(cmbSearchFields.Text))
                        {
                            lstKeywords.Items[i] = cmbSearchFields.Text + ":" + txtSearchKeywords.Text.Trim();
                        }
                    }
                }
                else
                {
                    lstKeywords.Items.Add(cmbSearchFields.Text + ":" + txtSearchKeywords.Text.Trim());
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string strTmpSelectString = strSelectWGJY;
            for (int i = 0; i < lstKeywords.Items.Count; i++)
            {
                string strTMpFieldName = lstKeywords.Items[i].ToString().Split(':').GetValue(0).ToString();
                string strTmpKeyword = lstKeywords.Items[i].ToString().Split(':').GetValue(1).ToString();
                if (strTmpSelectString.Contains(" where "))
                {
                    strTmpSelectString += " and ";
                }
                else
                {
                    strTmpSelectString += " where ";
                }
                switch (strTMpFieldName)
                {
                    case "型号规格":
                        strTmpSelectString += "型号规格 like '" + strTmpKeyword + "'";
                        break;
                    case "结余号":
                        strTmpSelectString += "结余号 like '" + strTmpKeyword + "'";
                        break;
                    case "生产批号":
                        strTmpSelectString += "批号 like '" + strTmpKeyword + "'";
                        break;
                    case "备注":
                        strTmpSelectString += "备注 like '" + strTmpKeyword + "'";
                        break;
                }
                if (cmbStatus.SelectedIndex != 0)
                {
                    if (strTmpSelectString.Contains(" where "))
                    {
                        strTmpSelectString += " and ";
                    }
                    else
                    {
                        strTmpSelectString += " where ";
                    }
                    switch (cmbStatus.SelectedIndex)
                    {
                        case 1:
                            strTmpSelectString += "数量=0";
                            break;
                        case 2:
                            strTmpSelectString += "数量>0";
                            break;
                    }
                    dgv1.DataSource = sqt.GetDataTable(strTmpSelectString);
                    dgv1.AutoResizeColumns();
                }
            }
        }

        private void frmWGKCSearch_Load(object sender, EventArgs e)
        {
            sqt = new SqlTools.SqlTools(WG计划管理工具.Properties.Settings.Default.ConnectionString);
        }

        private void 第一条ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgv1.Rows.Count > 0)
            {
                dgv1.CurrentCell = dgv1.Rows[0].Cells[0];
            }
        }

        private void 最后一条ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgv1.Rows.Count > 0)
            {
                dgv1.CurrentCell = dgv1.Rows[dgv1 .Rows.Count -1].Cells[0];
            }
        }

        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgv1.CurrentCell != null)
            {
                Clipboard.SetText(clsTools.GetString(dgv1.CurrentCell.Value));
            }
        }
    }
}